package com.glodon.ggzy.fwpt.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.JsonUtils;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.QuerySupport;
import com.glodon.ggzy.core.utils.Tools;
import com.glodon.ggzy.entity.GgCqLaw;
import com.glodon.ggzy.entity.GgJsLaw;
import com.glodon.ggzy.entity.GgTdLaw;
import com.glodon.ggzy.entity.GgZfLaw;
import com.glodon.ggzy.fwpt.service.ICqjyProjectService;
import com.glodon.ggzy.fwpt.service.IJsgcProjectService;
import com.glodon.ggzy.fwpt.service.ITdfcProjectService;
import com.glodon.ggzy.fwpt.service.IZfcgProjectService;
import com.glodon.ggzy.vo.JsgcInfoVO;
import com.glodon.ggzy.vo.Law;

/**
 * 
 * 法律法规处理控制器
 * @author yangjf
 * @date 2013/06/11
 */
@Controller
public class FlfgController {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IJsgcProjectService jsgcProjectService;

	@Autowired
	private ITdfcProjectService tdfcProjectService;

	@Autowired
	private ICqjyProjectService cqjyProjectService;

	@Autowired
	private IZfcgProjectService zfcgProjectService;

	/**
	 * 获取法律法规集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_list")
	public String getFlfgList(Model model) {
		return "/fwpt/flfg_list";
	}

	/**
	 * 获取法律法规-建设工程集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_jsgc_list")
	public String getFlfgJsgcList(Model model) {
		return "/fwpt/flfg_jsgc_list";
	}

	/**
	 * 获取法律法规-产权交易分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_cqjy_page_list")
	public String getCqjyPageList(Model model) {
		return "/fwpt/flfg_cqjy_page_list";
	}

	/**
	 * 获取法律法规-建设工程分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/get_flfg_cqjy_page_data")
	public void getFlfgCqjyPageList(Model model, int rows, int page, String lawName, String lawType, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		qm.addParamToMap(QuerySupport.QUERYCLASS, GgCqLaw.class);
		qm.addParamToMap(QuerySupport.ORDERBYDESC, "updatetime");

		if (StringUtils.isNotEmpty(lawName)) {
			try {
				qm.addParamToMap("name_like", "%" + URLDecoder.decode(lawName, "UTF-8") + "%");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		if (!StringUtils.isEmpty(lawType)) {
			String lawTypeText = "";
			if (lawType.equals("gjj")) {
				lawTypeText = "国家级";
			}
			if (lawType.equals("gds")) {
				lawTypeText = "广东省";
			}
			if (lawType.equals("zhs")) {
				lawTypeText = "珠海市";
			}
			qm.addParamToMap("lawtype", lawTypeText);
		}
		Page queryPage = cqjyProjectService.queryPage(qm);

		List<GgCqLaw> cqLawList = (List<GgCqLaw>) queryPage.getResult();
		List<Law> lawList = new ArrayList<Law>();
		Law law = null;
		for (GgCqLaw gg : cqLawList) {
			String gpDate = gg.getUpdatetime() == null ? "" : Tools.formatDateToString(gg.getUpdatetime());
			law = new Law(gg.getId(), gg.getName(), gg.getLawtype(), gpDate);
			lawList.add(law);
		}
		queryPage.setResult(lawList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取法律法规-产权交易分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_jsgc_page_list")
	public String getFlfgJsgcPageList(Model model) {
		return "/fwpt/flfg_jsgc_page_list";
	}

	/**
	 * 获取法律法规-产权交易分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_tdfc_page_list")
	public String getFlfgTdfcPageList(Model model) {
		return "/fwpt/flfg_tdfc_page_list";
	}

	/**
	 * 获取法律法规-建设工程分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_tdfc_page_data")
	public void getFlfg_TdfcPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		Page queryPage = tdfcProjectService.queryTdfcLaw(qm, searchText);
		List<GgTdLaw> lawList = (List<GgTdLaw>) queryPage.getResult();
		List<JsgcInfoVO> flfgList = new ArrayList<JsgcInfoVO>();
		for (GgTdLaw law : lawList) {
			JsgcInfoVO zbgg = new JsgcInfoVO(law.getId(), law.getName(), law.getLawtype(), Tools.formatDateToString(law.getUpdatetime()), law.getId());
			flfgList.add(zbgg);
		}
		queryPage.setResult(flfgList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取法律法规-政府采购分页集合
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_zfcg_page_list")
	public String getFlfgZfcgPageList(Model model) {
		return "/fwpt/flfg_zfcg_page_list";
	}

	/**
	 * 获取法律法规-政府采购
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_zfcg_page_data")
	public void getFlfg_ZfcgPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		Page queryPage = zfcgProjectService.queryZfcgLaw(qm, searchText);

		List<GgZfLaw> lawList = (List<GgZfLaw>) queryPage.getResult();

		List<JsgcInfoVO> flfgList = new ArrayList<JsgcInfoVO>();
		for (GgZfLaw law : lawList) {
			JsgcInfoVO zbgg = new JsgcInfoVO(law.getId(), law.getName(), law.getLawtype(), Tools.formatDateToString(law.getUpdatetime()), law.getId());
			flfgList.add(zbgg);
		}
		queryPage.setResult(flfgList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取法律法规-政府采购详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_zfcg_detail")
	public String getFlfgZfcgDetail(String id, Model model) {
		GgZfLaw law = zfcgProjectService.getGgZfLawById(id);
		model.addAttribute("law", law);
		return "/fwpt/flfg_zfcg_detail";
	}

	/**
	 * 获取法律法规-建设工程分页
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_jsgc_page_data")
	public void getZbgg_CqjyPageList(Model model, int rows, int page, String searchText, HttpServletResponse response) {
		PageSearchModel qm = new PageSearchModel();
		qm.setPageNo(page);
		qm.setPageSize(rows);
		if (!StringUtils.isEmpty(searchText)) {
			try {
				searchText = URLDecoder.decode(searchText, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		Page queryPage = jsgcProjectService.queryGgJsLaw(qm, searchText);
		List<GgJsLaw> lawList = (List<GgJsLaw>) queryPage.getResult();
		List<JsgcInfoVO> flfgList = new ArrayList<JsgcInfoVO>();
		for (GgJsLaw law : lawList) {
			JsgcInfoVO zbgg = new JsgcInfoVO(law.getId(), law.getName(), law.getLawtype(), Tools.formatDateToString(law.getUpdatetime()), law.getId());
			flfgList.add(zbgg);
		}
		queryPage.setResult(flfgList);
		String jsonStr = "";
		try {
			jsonStr = JsonUtils.toJsonStr(queryPage);
			response.getWriter().write(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取法律法规-建设工程详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_jsgc_detail")
	public String getFlfgJsgcDetail(String id, Model model) {
		GgJsLaw law = jsgcProjectService.getGgJsLawById(id);
		model.addAttribute("law", law);
		return "/fwpt/flfg_jsgc_detail";
	}


	/**
	 * 获取法律法规-产权交易详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_cqjy_detail")
	public String getFlfgCqjyDetail(String id, Model model) {
		GgCqLaw law = cqjyProjectService.getGgCqLawById(id);
		model.addAttribute("law", law);
		return "/fwpt/flfg_cqjy_detail";
	}



	/**
	 * 获取法律法规-土地房产详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/fwpt/flfg_tdfc_detail")
	public String getFlfgTdfcDetail(String id, Model model) {
		GgTdLaw law = tdfcProjectService.getGgTdLawById(id);
		model.addAttribute("law", law);
		return "/fwpt/flfg_tdfc_detail";
	}
}
